package cn.fl.dk.web.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.dk.facade.api.IDkQuestneLogApiService;
import cn.fl.dk.facade.constant.DkQuestneLogConstant;
import cn.fl.dk.facade.dto.DkQuestneLogDTO;
import cn.fl.dk.web.service.IDkBillDtlService;
import cn.fl.dk.web.service.IDkQuestneLogService;
import cn.fl.dk.web.transform.DkQuestneLogVOTransform;
import cn.fl.dk.web.transform.KeyValueVOTransform;
import cn.fl.dk.web.vo.DkQuestneLogVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysOrgService;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUsrVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: 问卷记录表(DK_QUESTNE_LOG)  questionnaire 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class DkQuestneLogServiceImpl implements IDkQuestneLogService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(DkQuestneLogServiceImpl.class);
	@Autowired
	private ISysDictionaryApiService sysDicApiService;
	@Autowired
	private IDkQuestneLogApiService dkQuestneLogApiService;
	@Autowired
	private ISysOrgService orgService ;
	@Autowired
	private ISysUsrService usrService;
	/**
	 * 垫款单明细
	 */
	@Autowired
	private IDkBillDtlService billDtlService;


	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Override
	public DkQuestneLogVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("主键参数不能为空!").setKeys("selectById",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		DkQuestneLogDTO dto = this.dkQuestneLogApiService.selectById(String.valueOf(id));
		return DkQuestneLogVOTransform.toVO(dto);
	}

	@Override
	public Long add(DkQuestneLogVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("新增对象不存在!").setKeys("add",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		}
		return this.dkQuestneLogApiService.add(DkQuestneLogVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("删除对象主键不能为空!").setKeys("removeById",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		}
		return this.dkQuestneLogApiService.removeDkQuestneLogDTOById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newLogger("删除主键参数集合不能为空!").setKeys("removeByIds",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		}
		return false;
    }

	@Override
	public boolean modify(DkQuestneLogVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("修改对象不能为空!").setKeys("modify",CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		}
		return this.dkQuestneLogApiService.modiy(DkQuestneLogVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager = this.dkQuestneLogApiService.searchListPage(page);
		List<DkQuestneLogVO> voList = DkQuestneLogVOTransform.toVOList((List<DkQuestneLogDTO>) pager.getResultList());
		// 省公司分公司查询
//		for (DkQuestneLogVO dkQuestneLogVO : voList) {
			// 查dk bill dtl
//			DkBillDtlVO dkBillDtlVO = billDtlService.selectByRuleFlag(dkQuestneLogVO.getRuleFlag());
//			dkQuestneLogVO.setCstMgrOrgNm(dkBillDtlVO.getCstMgrOrgNm());
//		}
		pager.setResultList(voList);
		return pager;
	}

	/**
	 * 根据groupCD 获取字典键值List
	 * @param groupCd
	 * @return List<KeyValueVO>
	 */
    @Override
    public List<KeyValueVO> getDictionaryList(String groupCd) {
		List<SysDictionaryDTO> sysDictionaryDTOS = this.sysDicApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), groupCd);
		return KeyValueVOTransform.toVoList(sysDictionaryDTOS);
	}

	@Override
	public DkQuestneLogVO selectByRuleFlag(String ruleId) {
		return DkQuestneLogVOTransform.toVO(this.dkQuestneLogApiService.selectByRuleFlag(ruleId));
	}

	@Override
	public void assignTask(String id, String usrId) {
		DkQuestneLogVO vo = this.selectById(Long.parseLong(id));
		SysUsrVO sysUsrVO = usrService.selectById(Long.parseLong(usrId),CurrentThreadContext.getCurrentSysCd());
		if(sysUsrVO!=null){
			vo.setCstMgrNm(sysUsrVO.getRefRlNm());
			vo.setCstMgrId(sysUsrVO.getId());
			vo.setCstMgrCode(sysUsrVO.getUsrNm());
			vo.setCstMgrOrgCd(sysUsrVO.getOrgCd());
			vo.setCstMgrState(DkQuestneLogConstant.ASSIGNED);
		}
		this.modify(vo);
	}
}
